package com.dyw.ycgovfinance.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.dyw.ycgovfinance.R;
import com.dyw.ycgovfinance.adapter.list.ListAdapterForOpenNews;
import com.dyw.ycgovfinance.bean.NewsListBean;
import com.dyw.ycgovfinance.commom.IntentKey;
import com.dyw.ycgovfinance.mvp.news.newsdetail.ActivityNewsDetail;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by clboys on 2017/6/29.
 */

public class ViewPagerAdapterOpenNewsYsJs extends PagerAdapter{



    Context ctx = null;
    //List<ListView> viewList = null;
    List<List<NewsListBean>> newsData;

    public ViewPagerAdapterOpenNewsYsJs(Context ct ,List<List<NewsListBean>> newsData) {

        ctx = ct;
        //viewList = views;
        this.newsData = newsData;

    }

    /**
     * Return the number of views available.
     */
    @Override
    public int getCount() {
        return newsData.size();
    }




    /**
     * Create the page for the given position.  The adapter is responsible
     * for adding the view to the container given here, although it only
     * must ensure this is done by the time it returns from
     * {@link #finishUpdate(ViewGroup)}.
     *
     * @param container The containing View in which the page will be shown.
     * @param position  The page position to be instantiated.
     * @return Returns an Object representing the new page.  This does not
     * need to be a View, but can be some other container of the page.
     */
    @Override
    public Object instantiateItem(
            ViewGroup container, int position) {
        View v = LayoutInflater.from(ctx).inflate(R.layout.view_open_news_page , null);
        ListView lv = (ListView) v.findViewById(R.id.lv_open_news_data);
        final List<NewsListBean> newsListBeen = newsData.get(position);
        ListAdapterForOpenNews newsAdapter = new ListAdapterForOpenNews(newsListBeen,ctx);
        lv.setAdapter(newsAdapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent toNewsDetail = new Intent((Activity)ctx,ActivityNewsDetail.class);
                toNewsDetail.putExtra(IntentKey.INTENT_KEY_NEWS_ID,String.valueOf(newsListBeen.get(position).getId()));
                ctx.startActivity(toNewsDetail);
            }
        });
        container.addView(v);
        return v;

    }

    /**
     * Remove a page for the given position.  The adapter is responsible
     * for removing the view from its container, although it only must ensure
     * this is done by the time it returns from {@link #finishUpdate(ViewGroup)}.
     *
     * @param container The containing View from which the page will be removed.
     * @param position  The page position to be removed.
     * @param object    The same object that was returned by
     *                  {@link #instantiateItem(View, int)}.
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }

    /**
     * Determines whether a page View is associated with a specific key object
     * as returned by {@link #instantiateItem(ViewGroup, int)}. This method is
     * required for a PagerAdapter to function properly.
     *
     * @param view   Page View to check for association with <code>object</code>
     * @param object Object to check for association with <code>view</code>
     * @return true if <code>view</code> is associated with the key object <code>object</code>
     */
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

}
